import java.util.Arrays;
import java.util.Random;

public class TestSort {

    //生成顺序数组
    public static void OrderArray(int[] arr) {
        for(int i = 0; i < arr.length; i++){
            arr[i] = i;
        }
    }

    //生成逆序数组
    public static void ReverseArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array.length-i;
        }
    }

    //生成随机数组
    public static void RandomArray(int[] array) {
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(array.length);
        }
    }

    public static void testInsertSort(int[] array) {
        int[] copyArray = Arrays.copyOf(array,array.length);

        long start = System.currentTimeMillis();
        Sort.insertSort(copyArray);
        long end = System.currentTimeMillis();
        System.out.println("插入排序:" + (end - start));
    }

    public static void testShellSort(int[] array) {
        int[] copyArray = Arrays.copyOf(array,array.length);

        long start = System.currentTimeMillis();
        Sort.shellSort(copyArray);
        long end = System.currentTimeMillis();
        System.out.println("希尔排序:" + (end - start));
    }

    public static void testSelectSort(int[] array) {
        int[] copyArray = Arrays.copyOf(array,array.length);

        long start = System.currentTimeMillis();
        Sort.selectSort(copyArray);
        long end = System.currentTimeMillis();
        System.out.println("选择排序:" + (end - start));
    }

    public static void testHeapSort(int[] array) {
        int[] copyArray = Arrays.copyOf(array,array.length);

        long start = System.currentTimeMillis();
        Sort.heapSort(copyArray);
        long end = System.currentTimeMillis();
        System.out.println("堆排序:" + (end - start));
    }

    public static void testBubbleSort(int[] array) {
        int[] copyArray = Arrays.copyOf(array,array.length);

        long start = System.currentTimeMillis();
        Sort.bubbleSort(copyArray);
        long end = System.currentTimeMillis();
        System.out.println("冒泡排序:" + (end - start));
    }

    public static void testQuickSort(int[] array) {
        int[] copyArray = Arrays.copyOf(array,array.length);

        long start = System.currentTimeMillis();
        Sort.quickSort(copyArray);
        long end = System.currentTimeMillis();
        System.out.println("快速排序:" + (end - start));
    }
    public static void testQuickSortNor(int[] array) {
        int[] copyArray = Arrays.copyOf(array,array.length);

        long start = System.currentTimeMillis();
        Sort.quickSortNor(copyArray);
        long end = System.currentTimeMillis();
        System.out.println("快速排序非递归:" + (end - start));
    }
    public static void testMergeSort(int[] array) {
        int[] copyArray = Arrays.copyOf(array,array.length);

        long start = System.currentTimeMillis();
        Sort.mergeSort(copyArray);
        long end = System.currentTimeMillis();
        System.out.println("归并排序:" + (end - start));
    }
    public static void testMergeSortNor(int[] array) {
        int[] copyArray = Arrays.copyOf(array,array.length);

        long start = System.currentTimeMillis();
        Sort.mergeSortNor(copyArray);
        long end = System.currentTimeMillis();
        System.out.println("归并排序非递归:" + (end - start));
    }

    //测试排序时间
    public static void main(String[] args) {
        int[] arr = new int[100000];
        //OrderArray(arr);
        //ReverseArray(arr);
        RandomArray(arr);

        //三傻排序（N^2）
        testInsertSort(arr);
        testSelectSort(arr);
        testBubbleSort(arr);

        testShellSort(arr);
        //N*logN
        testHeapSort(arr);
        testQuickSort(arr);
        testQuickSortNor(arr);
        testMergeSort(arr);
        testMergeSortNor(arr);
    }

    //测试排序是否正确
    public static void main2(String[] args) {
        int[] array = {4,1,5,7,2,8,3,9,6};

        //Sort.insertSort(array);
        //Sort.shellSort(array);
        //Sort.selectSort(array);
        //Sort.heapSort(array);
        //Sort.bubbleSort(array);
        //Sort.quickSort(array);
        //Sort.mergeSort(array);

        //非递归
        //Sort.quickSortNor(array);
        //Sort.mergeSortNor(array);
        System.out.println(Arrays.toString(array));
    }
}
